<?php
namespace Admin\Controller;
use Common\Controller\BaseController;

class PublicController extends BaseController{ 
	/**
	 * 是否锁定，2小时内不良记录是否达到5次，
	 */
	private function _index_userLock($info){
		if(!$info){
			$this->returnResult(false, '账号或密码错误');
		}
		//获取当前时间
	    $now=time();
	    //当前“时间戳”减去数据库里的“时间戳”
	    $cha=$now-$info['last_login_time'];	

		//不良记录达到5次，离最后登录时间未达2小时
		if($info['number'] >= 5 && $cha < 7200){
			$this->returnResult(false, '你的账号已锁定，请两个小时后登录！');
			return false;
		}
		//不良记录达到5次，最后登录时间超2小时，自动解锁；
		if($info['number'] > 0 && $cha > 7200){
			echo "aaa";
			M('admins')->where(array('id' => $info['id']))->save(array('number'=>0,'last_login_time' => time(), 'last_login_ip' => $ip));
		}
		return true;
	}    
	public function index(){
		$ip = get_client_ip();

		if(IS_POST){
			$yzImg = I('post.yzImg');
			if(strlen($yzImg) == 0){
				$this->returnResult(false, '验证码不能为空');
			}
			$authnum=I('session.login_check_number');
			if($yzImg != $authnum){
				$this->returnResult(false, '验证码不正确');
			}
			$name = I('post.username');
			$pwd = I('post.password');

			$info = M('admins')->field('id,gid,pwd,number,tel,last_login_time')->where(array('status' => 1, 'name' => $name))->find();
			//是否锁定，2小时内不良记录是否达到5次，
			$this->_index_userLock($info);
			if($info['pwd'] === md5($name . '_NiU_' . $pwd)){
				$rules = M('group')->where(array('id' => $info['gid']))->getField('rules');
				session('adminInfo', array('id' => $info['id'], 'name' => $name, 'tel' => $info['tel'], 'rules' => $rules, 'gid' => $info['gid']));				
				M('admins')->where(array('id' => $info['id']))->save(array('number'=>0,'last_login_time' => time(), 'last_login_ip' => $ip));
				M('admin_login_log')->add(array('name' => $name, 'times' => time(), 'ip' => $ip));	
								
				$this->returnResult(true,'登录成功', U('Index/index'));			
			}else{		
				M('admin_login_log')->add(array('name' => $name, 'times' => time(), 'ip' => $ip, 'status' => 0, 'pwd' => $pwd));				
				$_number=$info['number']+1;
				M('admins')->where(array('id' => $info['id']))->save(array('number'=>$_number,'last_login_time' => time(), 'last_login_ip' => $ip));
				$times=5-$_number;			
				if($times>0){
				  	$this->returnResult(false, '账号或密码错误！你还剩'.$times.'次机会');			 
				}else{
					M('admins')->where(array('id' => $info['id']))->save(array('number'=>$_number,'last_login_time' => time(), 'last_login_ip' => $ip));
					$this->returnResult(false,'账号或密码错误次数已达5次，账号将锁定2小时！');
				}                 		 		    		 
			} 			 	
		}else{

			if(session('adminInfo.id') > 0){
				$this->redirect('Admin/Index/index');
			}
			$this->display();
		}
	}

	public function logout(){
		session('[destroy]');
		$this->redirect('Admin/Public/index');
	}

	public function editPwd(){
		if(IS_POST){
			$pwd = M('admins')->where(array('id' => session('adminInfo.id')))->getField('pwd');
			if(md5(session('adminInfo.name') . '_NiU_' . I('post.old_pwd')) === $pwd){
				$data['id'] = session('adminInfo.id');
				$data['pwd'] = md5(session('adminInfo.name') . '_NiU_' . I('post.new_pwd'));
				$result = $this->updateData($data, 'admins', 2);
				$this->returnResult($result, null, U('Index/index'));
			}
			$this->returnResult(false, '原始密码错误');
		}else{
			$this->assign('menuList', session('left_menu'));
			$this->display();
		}
	}
}